<template>
  <div class="wrap wrapContent">
    <div style="width: 100%; height: 100vh;overflow:auto">
      <ul id="zTreeId" class="ztree"></ul>
    </div>
  </div>
</template>
<script>
import '@/assets/plugin/ztree/js/jquery-1.4.4.min.js';
import { levelList,fetchList2 ,getAllMonth,findZzjg} from "@/api/user";
import { getToken, removeToken } from "@/utils/auth"; // 驗權
import {getMonthPv2} from '@/api/pv'

//用户添加编辑页面
export default {
  data() {
    return {
      userLevel1:  ['普通會員', '經銷商', '副總監', '总监', '区域總監','高级区域總監', '亞洲總監', '亞太區總監', '環球總監'],
      setting: {
        view: {
          dblClickExpand: false
        },
        data: {
          simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "pid",
            rootPId: getToken(),
          },
        },
      },
      zNodes: [],
      //数据
      zTreeObj: null,
    };
  },
  //页面初始化参数加载
  mounted() {
    this.getLevelList();
    //页面初始化参数设置   
  },
  methods: {
      contains2(a, obj,key) {
        var i = a.length;
        while (i--) {
            if (a[i][key] == obj[key]) {

                return i;
            }
        }
        return false;
    },
    async getLevelList() {
        let all=await findZzjg({})
        all = all.data
          levelList({ userId: getToken() }).then(async (response) => {
            if (response.errno == 0) {
                this.zNodes = response.data;
                 let response3=await getAllMonth({})

                for (let index = 0; index < this.zNodes.length; index++) {
                  const element = this.zNodes[index];
                  element['monthPv']=0
                  
           
                  for (let i = 0; i < response3.length; i++) {
                    const element2 = response3[i];
                    if(element.id == element2.user_id){
                      element['monthPv']+=Number(element2.integral_price)
                    }
                  }
                  for (let i = 0; i < all.length; i++) {
                        const allitem = all[i];
                        if(this.contains2(this.zNodes,allitem,'id')==false && allitem.pid==element.id  ){
                            this.zNodes.push(allitem)
                        }
                    }
                  element.open=true
                  element['name']=element.surname+element.username+' '+' '+ ' '+this.userLevel1[element.post-1]+ ' 銷售PV  ' + (element.monthPv || 0)
                }
                this.zTreeObj = $.fn.zTree.init($("#zTreeId"), this.setting, this.zNodes);
            }
          });
    
    },
  },
};
</script>
<style >
.wrapContent {
  width: 80%;
  margin: 40px auto;
}
.ztree .ico_docu{display: none!important;}
.ztree .ico_open{ display: none!important;}
.ztree .ico_close{ display: none!important;}
</style>